package com.ai.controller.bt;

import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import javax.annotation.Resource;

import org.apache.commons.io.FileUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.ai.common.controller.BaseController;
import com.ai.common.model.Pagination;
import com.ai.common.model.Result;
import com.ai.mmd.reviewed.dto.JzxslinfoDTO;
import com.ai.mmd.reviewed.dto.SbhzinfoDTO;
import com.ai.model.member.info.dto.DataTablesDto;
import com.ai.service.reviewed.model.JzxslinfoModel;
import com.ai.service.reviewed.model.YhxxModel;
import com.ai.service.reviewed.service.IJzxslinfoService;
import com.ai.service.reviewed.service.ISbhzinfoService;

@RequestMapping("/btsb")
@Controller("com.ai.controller.bt.SlglController")
public class SlglController extends BaseController{
	
	@Value("${mainDomain}")
	private String mainDomain;
	
	/**
	 * Description ：列表引导页
	 */
	@RequestMapping("/addslglindex")
	public String addslglindex(@ModelAttribute SbhzinfoDTO sbhzinfoDTO)throws Exception {
		
		YhxxModel yhxxModel = (YhxxModel)request.getSession().getAttribute("yhxxModel");
		if (yhxxModel==null) {
			return "/front/frontgoods/login";
		}
		
		sbhzinfoDTO.setHzzt(0);
		List<Map<String, String>> hwydhlist = isbhzinfoService.getHwydhList(sbhzinfoDTO);
		
		Result result = new Result(true);
		result.addAttribute("hwydhlist", hwydhlist);
		result.rendering();
		
		return "/front/frontgoods/btsb/jzxslgl";
	}
	
	/**
	 * Description ：水路关联列表
	 */
	@RequestMapping("/slgllist")
	@ResponseBody
	public DataTablesDto<JzxslinfoModel> slgllist(@ModelAttribute JzxslinfoDTO jzxslinfoDTO)throws Exception {

		DataTablesDto<JzxslinfoModel> dto = new DataTablesDto<JzxslinfoModel>();
		
		YhxxModel yhxxModel = (YhxxModel)request.getSession().getAttribute("yhxxModel");
		if (yhxxModel==null) {
			List<JzxslinfoModel> list = new ArrayList<JzxslinfoModel>();
			dto.setRowsData(list);
			dto.setiTotalRecords(list.size());
			dto.setiTotalDisplayRecords(list.size());
			return dto;
		}
		
		//获得明细列表
		jzxslinfoDTO.setSfsc(0);
		jzxslinfoDTO.setYhid(yhxxModel.getYhid());
		Pagination<JzxslinfoModel> list = ijzxslinfoService.getPagination(jzxslinfoDTO);
		
		//存入集合
		dto.setRowsData(list.getRows());
		dto.setiTotalRecords(list.getTotal());
		dto.setiTotalDisplayRecords(list.getTotal());
		
		return dto;
	}
	
	/**
	 * 编辑货运单
	 * @param SbmxinfoDTO
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("edithyd")
	public void edithyd(@RequestParam(value = "hydfile") MultipartFile file,
			@ModelAttribute JzxslinfoDTO jzxslinfoDTO)
			throws Exception {
		
		YhxxModel yhxxModel = (YhxxModel)request.getSession().getAttribute("yhxxModel");
		if (yhxxModel==null) {
			return;
		}
		
		//判断新增或更新
		JzxslinfoModel jzxslinfoModel = ijzxslinfoService.getOne(jzxslinfoDTO);
		if (jzxslinfoModel!=null) {
			jzxslinfoDTO.setId(jzxslinfoModel.getId());
		}
		
		String hyd = uploadDoc(file);
		jzxslinfoDTO.setFjdz(hyd);
		ijzxslinfoService.edithyd(jzxslinfoDTO);
		
		response.setContentType("text/html;charset=utf-8");
        response.getWriter().write("{\"message\":\"Y\"}");
	}
	
	private String uploadDoc(MultipartFile articleFile)
			throws Exception {
		
		String baseFolder = this.getClass().getClassLoader()
				.getResource("../../upload/images").getPath();
		File folder = new File(baseFolder);
		
		String newFileName = UUID.randomUUID() + articleFile.getOriginalFilename().substring(articleFile.getOriginalFilename().lastIndexOf("."));
		
		FileUtils.copyInputStreamToFile(articleFile.getInputStream(),
				new File(folder,newFileName));
		return mainDomain + "/upload/images/" +  newFileName;
	}
	
	@Resource(name = "ijzxslinfoService")
	private IJzxslinfoService ijzxslinfoService;
	
	@Resource(name = "isbhzinfoService")
	private ISbhzinfoService isbhzinfoService;
}
